﻿using System.Data.SQLite;

namespace RobosimAPI.Class
{
	public class Account
	{
		public const string DATABASE = @"H:\桌面\RobosimAPI\RobosimAPI\DataBase\robosim.db";
		public const string DB_CONNECTION_STRING = $"Data Source={DATABASE};Pooling=True;Max Pool Size=20;";
		public int user_id;
		public string? account_name;
		public string? account_password;
		public int gender;
		public string? real_name;
		public string? school;
		public int is_baned;
		public int vip;
		public string? token;
		public string? face_url;
		public Account(int user_id)
		{
			using (var conn = new SQLiteConnection(DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand("SELECT * FROM account WHERE user_id = @user_id LIMIT 1", conn);
				cmd.Parameters.AddWithValue("@user_id", user_id);
				var reader = cmd.ExecuteReader();
				if (reader.Read())
				{
					this.user_id = reader.GetInt32(0);
					account_name = reader.GetString(1);
					account_password = reader.GetString(2);
					gender = reader.GetInt32(3);
					real_name = reader.GetString(4);
					school = reader.GetString(5);
					is_baned = reader.GetInt32(6);
					vip = reader.GetInt32(7);
					token = reader.GetString(8);
					face_url = reader.IsDBNull(9) ? null : reader.GetString(9);
				}
				conn.Close();
				conn.Dispose();
			}
		}
		public Account(string account_name)
		{
			using (var conn = new SQLiteConnection(DB_CONNECTION_STRING))
			{
				conn.Open();				
				var cmd = new SQLiteCommand("SELECT * FROM account WHERE account_name = @account_name LIMIT 1", conn);
				cmd.Parameters.AddWithValue("@account_name", account_name);
				var reader = cmd.ExecuteReader();
				if (reader.Read())
				{
					user_id = reader.GetInt32(0);
					this.account_name = reader.GetString(1);
					account_password = reader.GetString(2);
					gender = reader.GetInt32(3);
					real_name = reader.GetString(4);
					school = reader.GetString(5);
					is_baned = reader.GetInt32(6);
					vip = reader.GetInt32(7);
					token = reader.IsDBNull(8) ? "" : reader.GetString(8);
					face_url = reader.IsDBNull(9) ? null : reader.GetString(9);
				}
				conn.Close();
				conn.Dispose();
			}
		}
		public enum Gender
		{
			Undefined,
			Boy,
			Girl
		}	
		public bool IsBaned()
		{
			if (is_baned == 1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		public Gender GetGender()
		{
			return (Gender)gender;
		}
		public bool IsValid()
		{
			if (account_name == null || account_password == null || real_name == null || school == null)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		public string GetPasswordMD5()
		{
			return Tools.GetMD5FromString(account_password);
		}
		public void UpdateToken()
		{
			using (var conn = new SQLiteConnection(DB_CONNECTION_STRING))
			{
				conn.Open();				
				var cmd = new SQLiteCommand("UPDATE account SET token = @token WHERE user_id = @user_id", conn);
				cmd.Parameters.AddWithValue("@token", token);
				cmd.Parameters.AddWithValue("@user_id", user_id);
				cmd.ExecuteNonQuery();
				conn.Close();
				conn.Dispose();
			}
		}
		public bool SingnCheck(string sign,string time)
		{
			return (Tools.GetSign(token, user_id.ToString(), time) == sign);
		}
		public bool IsVip()
		{
			return (vip > 0);
		}
		public int[] GetAllJoinedEvents()
		{
			using (var conn = new SQLiteConnection(DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand("SELECT * FROM join_list WHERE user_id = @user_id", conn);
				cmd.Parameters.AddWithValue("@user_id", user_id);
				var reader = cmd.ExecuteReader();
				int[] events = new int[reader.FieldCount];
				int i = 0;
				while (reader.Read())
				{
					events[i] = reader.GetInt32(1);
					i++;
				}
				conn.Close();
				conn.Dispose();
				return events;
			}	
		}
	}
}
